require.css( './editor.css' );
require.css( './show.css' );

var editor = {
	vm: function() {
		return VM( '/index/mod_editor' );
	},
	dir : {
		type_dir : 'D',
		type_file : 'F',
		enterDir : function(target, dirId, reloadMenu) {
			var vm = VM(target);
			if (!reloadMenu) { // 默认要刷新菜单
				reloadMenu = '1';
			}
//			vm.cmd('enterDir', dirId, vm.data('fromModule'));
			// editorDir/enterDir?dirId=$0&fromModule=$1&reloadMenu=$2
//			if (reloadMenu == '1') {
//				// 刷新树
//			}
			var fromModule = vm.data('fromModule');
			vm.cmd({ type:'ajax', src:'editorDir/enterDir?dirId='+dirId+'&fromModule='+fromModule});
			window.location.href='index.jsp#'+fromModule+"@folder@"+dirId;
//			target.cmd({ type:'ajax', src:'editorDir/enterDir?dirId='+dirId+'&fromModule='+VM(target).fv('fromModule'), });
		},
		openToMenu : function(target, targetDir) {
			var vm = VM(target);
			if (!targetDir) {
				targetDir = vm.data('targetDir');
			}
			vm.find('b_menu').openTo('editorDir/openTo?targetDir=' + targetDir);
		},
		enterEditor : function(target, viewId) {
			var vm = VM(target);
			var fromModule = vm.data('fromModule');
			vm.cmd({type:'ajax', src:'editor/reload?viewId=' + viewId + '&fromModule=' + fromModule});
			window.location.href='index.jsp#'+fromModule+"@file@"+viewId;
		},
		enter : function(target, itemId, itemType) {
			if (itemType == this.type_file) { // 文件
				this.enterEditor(target, itemId);
			} else { // 其他都是目录(包含用户目录)
				this.enterDir(target, itemId);
				var leaf=VM(target).find(itemType+itemId);
				if (leaf) {
					leaf.focus();
				}
			}
		},
		checkItem : function(target) {
			var vm = VM(target);
			var selItem = vm.fv('selItem');
			vm.find('btnDel').disable(!selItem);
			vm.fv('selectItem', selItem);
		},
		checkAllItem : function(target) {
			VM(target).find('m_items').focusAll(target.isChecked());
			this.checkItem(target);
		},
		delItem : function(target) {
			var vm = VM(target);
			vm.cmd('delete', vm.fv('selectItem'), vm.data('targetDir'));
		},
		edit : function(target, itemId, itemType) {
			var vm = VM(target);
//			var dlgTitle = '';
//			if (editType == this.type_editor) {
//				dlgTitle = '编辑器';
//			} else {
//				dlgTitle = '目录';
//			}
			vm.cmd('edit',itemId,itemType,vm.data('targetDir'),vm.data('fromModule'));
		},
		itemMenu : function(target, itemId, itemType, posIndex) {
			var vm = VM(target);
			var selectItem = vm.fv('selectItem');
			var itemArray = selectItem.split(',');
			var currItem = itemType+itemId;
			var isSelectCurr = false;
			for (var i=0; i<itemArray.length; i++) {
				var item = itemArray[i];
				if (currItem == item) {
					isSelectCurr = true;
					break;
				}
			}
			// FIXME 目前没处理多文件,暂时一个文件
			vm.find('m_items').focusAll(false);
			target.focus();
			
			target.cmd({type:'ajax', src:'editorDir/itemMenu?itemId='+itemId+'&itemType='+itemType+'&posIndex='+posIndex+'&fromModule='+vm.data('fromModule')});
		},
		dirMenu : function(target) {
			var vm = VM(target);
			vm.cmd({ type:'ajax', src:'editorDir/dirMenu?targetDir=' + vm.data('targetDir')});
		},
		reorder : function(target, dirId, orderType) {
			var vm = VM(target);
			if (!dirId) {
				dirId = vm.data('targetDir');
			}
			vm.cmd('reorder', dirId, orderType);
		},
		dirFormat : function(id, text, dis) {
			var html = '';
			if (dis == '1') {
				html = "<div style='color:grey;'>" + text + "</div>";
			} else {
				html = text;
			}
			return html;
		},
		selDir : function(target) {
			var vm = VM(target);
			var dis = target.data('dis');
			if (dis == '1') {
				vm.fv('toDir', '');
				vm.find('btnSave').disable(true);
				target.toggle();
			} else {
				var dirId = target.x.id;
				vm.fv('toDir', dirId);
				vm.find('btnSave').disable(false);
			}
		}
	},
	file : { // FIXME 这个名字有点怪异,需要给个好名分
		highlight : function( leaf, viewId ) {
			var target = $.globals[ leaf.x.id ],
				c = target && $.bcr( target );
			if ( c && c.width && c.height ) {
				this.highlightRemove();
				var e = $.db( '<div id=' + leaf.id + '_guide class=guide-box style="width:' + Math.max(50, c.width - 2) + 'px;height:' + Math.max(20, c.height - 2) + 'px;top:' + (c.top - 1) + 'px;left:' + (c.left - 1) + 'px">' +
					'<div class=guide-x onclick=editor.file.highlightRemove()>&times;</div><div class=guide-more onclick=editor.file.nodeMenu(this,"' + target.x.gid + '","' + viewId + '")>' + $.arrow( 'b2' ) + '</div>' +
					'</div>' );
				this.highlightBlink( e );
			}
		},
		highlightRemove : function() {
			Q( '.guide-box' ).remove();
			Q( document ).off( 'click.guide' );
		},
		highlightBlink : function ( a, b ) {
			setTimeout( function() {
				$.classAdd( a, 'z-on' );
				var i = 0, t = setTimeout( function() {
					$.classAdd( a, 'z-on', i % 2 == 1 );
					if ( i ++ > 1 ) {
						clearTimeout( t );
						Q( document ).on( 'click.guide', function(e) {
							if ( Q( '.guide-box' ).has( e.target ).length === 0 )
								editor.file.highlightRemove();
						} );
					} else
						t = setTimeout( arguments.callee, 200 );
				}, 200 );
			}, 50 );
		},
		nodeMenu : function(target, nodeId, viewId) {
			if (!nodeId) {
				// FIXME 新建的参数还需要补充
				nodeId = 'NEW';
			}else if (nodeId=='compTree'){
				nodeId='0000000000000000';
			}
			var vm = VM(target);
			var fromModule = vm.data('fromModule');
			vm.cmd({type:'ajax', src:'editor/nodeMenu?nodeId='+nodeId+'&viewId=' + viewId + '&fromModule=' + fromModule});
		},
		reloadEditor : function(target, viewId) {
			target.cmd({type:'ajax', src:'editor/reload?viewId=' + viewId });
		},
		addProp : function(partId,target) {
			var vm = VM(target);
			if (!vm) {
				return;
			}
			var index = vm.data(partId+'_index');
			if (!index) {
				index = vm.fv(partId+'_index');
			}
			if (!index) {
				index = 1;
			} else {
				index++;
			}
			vm.data(partId+'_index', index);
			var prefix="D";
			if("event"==partId){
				prefix="E";
			}else if("hiddens"==partId){
				prefix="H";
			}else if("options"==partId){
				prefix="P";
			}
			var rowData = {
					id:partId+"_row_"+index,
					data:{
					       "K":{"type":"text", "name":prefix+"K_"+index,"width":"*","placeholder":"请输入关键字","transparent":true},
					       "V":{"type":"text", "name":prefix+"V_"+index,"width":"*","placeholder":"请输入内容","transparent":true},
					       "O":{"type":"html", "text":"&nbsp;<a href='javascript:;' onclick=\"VM(this).find('"+partId+"_row_"+index+"').remove()\">删除&nbsp;</a>","width":"*","align":"center"}
					}
			};
			if("E"==prefix){
				rowData.data.K={"type":"onlinebox", "name":prefix+"K_"+index,"width":"*","placeholder":"请输入关键字","transparent":true,"src":"editor/eventOptions?key=$text"};
			}
			VM(target).find('ct_'+partId).insertRow(rowData);
		},
		copyNode : function(target, nodeId, copyType) {
			
		},
		pasteNode : function(target, nodeId) {
			
		},
		stack: function(a){
			var btn = $.widget(a);
			var subLayout = btn.parentNode;
			var layout = subLayout.parentNode;
			var focus = parseFloat(layout.data('focus'));
			if( subLayout.nodeIndex != focus ) {
				layout[focus].height(layout.data('labelHeight'));
				subLayout.height('*');
				layout.data('focus', subLayout.nodeIndex);
			}
		},
		versionFormat : function (rowViewId,viewId){
			var html="[<a href='javascript:' onclick=\"$.confirm('你确定要恢复此版本吗？',VM(this).cmd({type:'ajax',src:'editor/recoveryVersion?viewId=" + rowViewId + "'}))\">恢复</a>]";
			if(rowViewId==viewId){
				html+='<span style="color:grey;">查看</span>';
			}else{
				html+="[<a href='javascript:' onclick=\"editor.dir.enterEditor(VM(this),'"+ rowViewId +"');\">查看</a>]";
			}
			return html;
		}
		
	}
};


// 导出模块
module.exports = editor;